package com.trg.flex.frm.validation
{
	import mx.controls.Alert;
	import mx.utils.StringUtil;
	import mx.validators.ValidationResult;
	import mx.validators.Validator;

	public class ArrayValidator extends Validator
	{
		public function ArrayValidator()
		{
			super();
		}
		
		public var minLength:Object;
		public var maxLength:Object;
		
		override protected function doValidation(value:Object):Array {
			var results:Array = super.doValidation(value);
			
			if (!(value is Array)) {
				
				results.push(new ValidationResult(
					true, null, "notArray", "Value is not an Array. It must be an array."));
									
			} else {
			
				if (minLength != null && minLength > 0 && (value as Array).length < minLength ) {
					results.push(new ValidationResult(
						true, null, "tooShort",
						StringUtil.substitute("Not enough items are selected. At least {0} items must be selected.", minLength)));
				}
				
				if (maxLength != null && maxLength > -1 && (value as Array).length > maxLength ) {
					results.push(new ValidationResult(
						true, null, "tooLong",
						StringUtil.substitute("Too many items are selected. At most {0} items may be selected.", maxLength)));
				}
			}
				
			return results;
		}
	}
}